﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MINERP.DAL;
namespace MINERP.BLL.InfoConfig
{
    public class AlarmReceiverBLL
    {
        public List<users> GetUserList()
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                return entities.users.Include("AlarmReceiver").Where(u=>u.IsUseful ==true).ToList();
            }
        }

        public void Add(string userName)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                
                users user = entities.users.FirstOrDefault(u => u.UserName == userName);
                if (!user.AlarmReceiver.IsLoaded)
                {
                    user.AlarmReceiver.Load();
                }
                if (user.AlarmReceiver.Count < 1)
                {
                    AlarmReceiver alarm = new AlarmReceiver();

                    alarm.users = user;
                    entities.AddToAlarmReceiver(alarm);
                    entities.SaveChanges();
                }
            }
        }

        public void Delete(string userName)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                users user = entities.users.FirstOrDefault(u => u.UserName == userName);
                if (!user.AlarmReceiver.IsLoaded)
                {
                    user.AlarmReceiver.Load();
                }
                if (user.AlarmReceiver.Count > 0)
                {
                    entities.DeleteObject(user.AlarmReceiver.First());
                    entities.SaveChanges();
                }
            }
        }
    }

}
